package edu.zhku.ttms.view.panel.component;

import javax.swing.*;
import java.awt.event.ItemEvent;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Feng
 * @since 2021/4/27 9:02
 */
public class DateTimePanel extends JPanel {

    JComboBox<String> yearInput;

    JComboBox<String> monthInput;

    DayOfMonthChooser dayInput;

    JComboBox<String> hourInput;

    JComboBox<String> minuteInput;

    public DateTimePanel() {
        this(new Date());
    }

    public DateTimePanel(Date date) {

//        System.out.println(date);

        setLayout(null);
//        setSize(375, 25);

        int defaultWidth = 70;
        int defaultHeight = 25;
        int margin = 5;

        int incrementX = defaultWidth + margin;

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
//        System.out.println(calendar);

        yearInput = new JComboBox<>();
        int year = calendar.get(Calendar.YEAR);
        yearInput.addItem(year - 1 + "年");
        yearInput.addItem(year + "年");
        yearInput.setSelectedIndex(1);
        yearInput.setBounds(0, 0, defaultWidth, defaultHeight);
        this.add(yearInput);

        monthInput = new JComboBox<>();
        int month = calendar.get(Calendar.MONTH);
        for (int i = 1; i <= 12; i++) {
            monthInput.addItem(i + "月");
        }
        monthInput.setSelectedIndex(month);
        monthInput.addItemListener(e -> {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                this.setVisible(false);
                this.remove(dayInput);
                calendar.set(selectedItem2Int(yearInput), selectedItem2Int(monthInput) - 1, 1);
                dayInput = new DayOfMonthChooser(calendar);
                dayInput.setBounds(incrementX * 2, 0, defaultWidth, defaultHeight);
                this.add(dayInput);
                this.setVisible(true);
            }
        });
        monthInput.setBounds(incrementX, 0, defaultWidth, defaultHeight);
        this.add(monthInput);

        dayInput = new DayOfMonthChooser(calendar);
        dayInput.setBounds(incrementX * 2, 0, defaultWidth, defaultHeight);
        this.add(dayInput);

        hourInput = new JComboBox<>();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        for (int i = 0; i < 24; i++) {
            hourInput.addItem(i + "时");
        }
        hourInput.setSelectedItem(hour + "时");
        hourInput.setBounds(incrementX * 3, 0, defaultWidth, defaultHeight);
        this.add(hourInput);

        minuteInput = new JComboBox<>();
        int minute = calendar.get(Calendar.MINUTE);
        for (int i = 0; i < 60; i++) {
            minuteInput.addItem(i + "分");
        }
        minuteInput.setSelectedItem(minute + "分");
        minuteInput.setBounds(incrementX * 4, 0, defaultWidth, defaultHeight);
        this.add(minuteInput);

    }

    public static <T> int selectedItem2Int(JComboBox<T> comboBox) {
        String selectedItemStr = String.valueOf(comboBox.getSelectedItem());
        return Integer.parseInt(selectedItemStr.substring(0, selectedItemStr.length() - 1));
    }

    public Date getTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, selectedItem2Int(yearInput));
        calendar.set(Calendar.MONTH, selectedItem2Int(monthInput) - 1);
        calendar.set(Calendar.DAY_OF_MONTH, selectedItem2Int(dayInput));
        calendar.set(Calendar.HOUR_OF_DAY, selectedItem2Int(hourInput));
        calendar.set(Calendar.MINUTE, selectedItem2Int(minuteInput));
        return calendar.getTime();
    }

}

class DayOfMonthChooser extends JComboBox<String> {
    public DayOfMonthChooser(Calendar calendar) {
        int maxDayOfMoth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int i = 1; i <= maxDayOfMoth; i++) {
            this.addItem(i + "日");
        }
        this.setSelectedItem(calendar.get(Calendar.DAY_OF_MONTH) + "日");
    }
}
